import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);
import category from "@/views/category/category.vue";
import home from "@/views/home/home.vue";
import shopcart from "@/views/shopcart/shopcart.vue";
import user from "@/views/user/user.vue";
import productList from '@/views/product-list/product-list.vue';
import productDetail from '@/views/product-detail/product-detail.vue'

import store from "@/store";


const routes = [{

        // redirect: '/category',
        path: "/",
        component: category,
        meta: {
            index: 1
        },
    },
    {
        path: "/home",
        component: home,
        meta: {
            index: 1
        },
    },
    {
        path: "/shopcart",
        component: shopcart,
        meta: {
            index: 1,
            auth: true
        },
    },
    {
        path: "/user",
        component: user,
        meta: {
            index: 1,
            auth: true
        },
    },
    // {
    //     path: '/register',
    //     name: 'register',
    //     component: () =>
    //         import ('@/views/login/register.vue'),
    //     meta: {
    //         index: 1
    //     }
    // },
    {
        path: '/product-list',
        component: productList,
        meta: {
            index: 2
        },
    },
    {
        path: '/login',
        name: 'Login',
        component: () =>
            import ('@/views/login/login.vue'),
        meta: {
            index: 2
        }
    },
    {
        name: "Detail",
        path: '/product/:id',
        component: productDetail
    },
    {
        path: '/proxy',
        name: 'Proxy',
        component: () =>
            import ('@/views/login/proxy.vue')
    }

];

const router = new VueRouter({
    routes,
});

router.beforeEach((to, from, next) => {
    // 判断当前用户 已登录 再次访问 登录界面 直接跳转到 个人中心页面
    if (to.name === 'Login' && store.getters.getIsLogin) {
        next('/user')
    }

    // console.log('to:', to);
    // console.log('from:', from);
    if (to.meta.auth) { // 需要校验用户是否登录的页面

        // 判断 用户是否登录
        // console.log(store.getters);
        if (store.getters.getIsLogin) {
            // console.log('------');
            next()
        } else {
            // console.log('+++++++');
            // 未登录 跳转到登录页面
            next({ path: '/login' })
        }
    } else {
        next()
    }
})


export default router;